<template>
  <div class="product-edit">
    <el-dialog v-bind="$attrs" :title="state.dialogTitle" v-model="state.modalVisible" :before-close="cancelDialog"
      :append-to-body="true" :destroy-on-close="true" :close-on-click-modal="false" :close-on-press-escape="false"
      :lock-scroll="false" class="product-dialog" draggable>
      <el-form ref="productRef" :model="state.form" :rules="state.rules" label-width="130px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="产品名称" prop="productname">
              <el-input v-model="state.form.productname" placeholder="产品名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品类型" prop="productlx">
              <el-input v-model="state.form.productlx" placeholder="产品类型" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品金额" prop="productprice">
              <el-input v-model="state.form.productprice" placeholder="产品金额" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="充值条数" prop="productnum">
              <el-input v-model="state.form.productnum" placeholder="充值条数" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品备注" prop="productbz">
              <el-input v-model="state.form.productbz" placeholder="产品备注" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品开始有效时间" prop="productstartdate">
              <el-input v-model="state.form.productstartdate" placeholder="产品开始有效时间" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品结束有效时间" prop="productenddate">
              <el-input v-model="state.form.productenddate" placeholder="产品结束有效时间" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品可用状态" prop="productstatus">
              <el-input v-model="state.form.productstatus" placeholder="产品可用状态" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitDialog">确 定</el-button>
          <el-button @click="cancelDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import useProductManageStore from '@store/modules/productManage'
import { filterObject } from '@/utils/index'
import { nextTick } from 'vue';

const productManageStore = useProductManageStore()
const { proxy } = getCurrentInstance()
const productRef = ref(null)

const state = reactive({
  modalVisible: false,
  dialogTitle: '',
  editView: {},
  form: {
    productname: '',
    productlx: '',
    productprice: '',
    productnum: '',
    productbz: '',
    productstartdate: '',
    productenddate: '',
    productstatus: '',
  },
  rules: {
    // account: [{ required: true, message: '账号不能为空', trigger: 'blur' }],
    // username: [{ required: true, message: '姓名/昵称不能为空', trigger: 'blur' }],
    // userworkgroup1: [{ required: true, message: '关联公司不能为空', trigger: 'blur' }],
    // userworkgroup2: [{ required: true, message: '关联部门不能为空', trigger: 'blur' }],
  },
})

const emits = defineEmits(['refresh'])

const getDataView = (productid) => {
  nextTick(async () => {
    const result = await productManageStore.view({ "productid.Value": productid })
    state.editView = result
    state.form = filterObject(result, Object.keys(state.form))
  })
}

const submitDialog = () => {
  productRef.value.validate(async (valid) => {
    if (valid) {
      if (Object.keys(state.editView).length) {
        state.form.productid = state.editView.productid
        await productManageStore.up(state.form)
        proxy.$modal.msgSuccess('修改成功！')
      } else {
        await productManageStore.add(state.form)
        proxy.$modal.msgSuccess('新增成功！')
      }
      state.editView = {}
      productRef.value.resetFields()
      emits('refresh')
      state.modalVisible = false
    }
  })
}

const cancelDialog = () => {
  state.editView = {}
  productRef.value.resetFields()
  state.modalVisible = false
}

const openDialog = async (data = {}) => {
  if (Object.keys(data).length) await getDataView(data.productid)
  state.modalVisible = true
  state.dialogTitle = Object.keys(data).length ? `编辑【${data.productname}】` : '新增'
}

defineExpose({
  openDialog
})
</script>

<style lang="scss" scoped>
@import './index.scss';
</style>
